<?php

namespace app\stores\controller\user;

use think\Controller;
use think\Request;

class Base extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel=model('User.User');
    }

    public function register(){
        if($this->request->isPost()){
            $params=$this->request->param();
            $email=$this->request->param('email','');

            $em = $this->UserModel->where(['email'=>$email])->find();
            if($em){
                $this->error('该用户已存在，请重新输入');
                exit;
            }

            $password=$this->request->param('password','');

            if(empty($password)){
                $this->error('密码不能为空');
                exit;
            }

            $salt=randstr(10);

            $password=md5(trim($password).$salt);

            $RegisterPoint=model('Config')->where(['key'=>'RegisterPoint'])->value('value');

            $RegisterPoint=is_numeric($RegisterPoint) ? abs($RegisterPoint) : 0;

            $data=[
                'email'=>$params['email'],
                'password'=>$password,
                'salt'=>$salt,
                'nickname'=>$params['email'],
                'point'=>$RegisterPoint,
                'vip'=>0,
                'auth'=>0
            ];

            $result=$this->UserModel->validate('common/User/User')->save($data);

            if($result === FALSE){
                $this->error($this->UserModel->getError());
                exit;
            }else{
                $this->success('注册成功');
                exit;
            }
        }
    }

    public function login(){
        if($this->request->isPost()){
            $email=$this->request->param('email','');
            $password=$this->request->param('password','');

            $User=$this->UserModel->where(['email'=>$email])->find();

            if(!$User){
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $salt=$User['salt'];
            $password=md5(trim($password).$salt);
            if($password != $User['password']){
                $this->error('密码输入有误，请重新输入');
                exit;
            }

            $data=[
                'id'=>$User['id'],
                'email'=>$User['email'],
                'nickname'=>$User['nickname'],
                'avatar'=>$User['avatar_cdn'],
                'province'=>$User['province'],
                'city'=>$User['city'],
                'district'=>$User['district'],
                'createtime'=>$User['createtime'],
                'content'=>$User['content'],
                'point'=>$User['point'],
                'vip'=>$User['vip'],
                'auth'=>$User['auth'],
                'cover'=>$User['cover_cdn'],
                'region'=>$User['region'],
            ];
            $this->success('登录成功','/',$data);
            exit;
        }
    }

    public function check()
    {
        //判断是否是post请求
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);

            // 查询这个用户是否存在
            $user = $this->UserModel->find($userid);

            if($user)
            {
                //设置一个保存cookie信息
                $data = [
                    'id'=>$user['id'],
                    'email'=>$user['email'],
                    'nickname'=>$user['nickname'],
                    'avatar'=>$user['avatar_cdn'],
                    'sex'=>$user['sex'],
                    'province'=>$user['province'],
                    'city'=>$user['city'],
                    'district'=>$user['district'],
                    'createtime'=>$user['createtime'],
                    'content'=>$user['content'],
                    'point'=>$user['point'],
                    'vip'=>$user['vip'],
                    'auth'=>$user['auth'],
                    'cover'=>$user['cover_cdn'],
                    'region'=>$user['region'],
                ];

                $this->success('登录成功，存在用户', null, $data);
                exit;
            }else
            {
                $this->error('用户不存在');
                exit;
            }
        }
    }

    public function profile(){
        if($this->request->isPost()){
            //获取所有的参数
            $params = $this->request->param();

            $userid = $this->request->param('id', 0);
            $password = $this->request->param('password', '');

            //先判断这个人是否存在
            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            //组装数据
            $data = [
                'id'=>$userid,
                'nickname'=>$params['nickname'],
                'email'=>$params['email'],
                'sex'=>$params['sex'],
                'content'=>$params['content'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
            ];

            //判断是否修改了邮箱
            if($user['email'] != $params['email'])
            {
                $data['auth'] = 0;
            }

            //密码不为空
            if(!empty($password))
            {
                $salt = $user['salt'];

                $repass = md5($password.$salt);

                //判断新密码和旧密码是否一致
                if($repass == $user['password'])
                {
                    $this->error('当前密码为旧密码，请重新输入');
                    exit;
                }

                //生成新的密码盐
                $newsalt = randstr(10);

                //新密码加密
                $password = md5(trim($password).$newsalt);

                $data['salt'] = $newsalt;
                $data['password'] = $password;
            }

            //判断头像
            if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0)
            {
                $result = upload('avatar');

                if($result === FALSE)
                {
                    $this->error('修改头像失败');
                    exit;
                }

                //追加头像
                $data['avatar'] = 'uploads/'.$result;
            }

            //封面
            if(isset($_FILES['cover']) && $_FILES['cover']['size'] > 0)
            {
                $result = upload('cover');

                if($result === FALSE)
                {
                    $this->error('修改头像失败');
                    exit;
                }

                //追加头像
                $data['cover'] = 'uploads/'.$result;
            }

            //更新数据库
            $result = $this->UserModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                //上传上来的图片删除了
                @is_file($data['avatar']) && @unlink($data['avatar']);

                @is_file($data['cover']) && @unlink($data['cover']);

                $this->error('修改个人资料失败');
                exit;
            }else
            {
                //先判断是否有选择新的图片
                if(@is_file($data['avatar']))
                {
                    //修改成功后，将原来的图片删除
                    $avatar = ltrim($user['avatar'],"/");
                    @is_file($avatar) && @unlink($avatar);
                }

                //先判断是否有选择新的图片
                if(@is_file($data['cover']))
                {
                    //修改成功后，将原来的图片删除
                    $cover = ltrim($user['cover'],"/");
                    @is_file($cover) && @unlink($cover);
                }

                //直接查询最新的结果在返回
                $UserData = $this->UserModel->find($userid);

                $data = [
                    'id'=>$UserData['id'],
                    'email'=>$UserData['email'],
                    'nickname'=>$UserData['nickname'],
                    'avatar'=>$UserData['avatar_cdn'],
                    'sex'=>$UserData['sex'],
                    'province'=>$UserData['province'],
                    'city'=>$UserData['city'],
                    'district'=>$UserData['district'],
                    'createtime'=>$UserData['createtime'],
                    'content'=>$UserData['content'],
                    'point'=>$UserData['point'],
                    'vip'=>$UserData['vip'],
                    'auth'=>$UserData['auth'],
                    'cover'=>$UserData['cover_cdn'],
                    'region'=>$UserData['region'],
                ];

                $this->success('修改资料成功', '/', $data);
                exit;
            }
        
        }
    }

    // 邮箱验证
    public function email()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);

            //判断用户是否存在
            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            //判断是否认证过
            if($user['auth'] == 1)
            {
                $this->error('您已经通过认证，无须重复认证');
                exit;
            }

            if(empty($user['email']))
            {
                $this->error('邮箱不能为空');
                exit;
            }


            //发送邮箱验证
            //将邮箱进行md5加密
            $email = md5($user['email']);

            //组装信息
            $msg = "<a href='http://www.ask.com/home/index/email?email=$email'>点击免费邮箱验证</a>";


            //调用公共函数库里面邮箱发送方法
            $result = send($user['email'], $msg);

            //返回的结果
            $success = [
                'result'=>false,
                'msg'=>''
            ];

            if($result)
            {
                $success['result'] = true;
                $success['msg'] = '发送邮件成功，请注意查收';
            }else
            {
                $success['result'] = false;
                $success['msg'] = '邮件发送失败，请重新发送';
            }

            return json($success);
        }
    }
}
